package com.heaven.customerServer.customerController;

import cn.dev33.satoken.stp.StpUtil;
import com.heaven.customerServer.customerController.vo.video.*;
import com.heaven.service.VideoService;
import com.heaven.utils.R;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("heaven-server/video")
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Resource
    private LoginController loginController;

    /**
     * 首页推荐视频
     */
    @GetMapping("/recommend")
    public R recommendVideos() {
        List<RecommendVideosRes> ans = videoService.recommendVideos();
        return R.okData(ans);
    }

    /**
     * 先把该表单提交后再触发上传任务
     * @param req 视频实体表单
     * @return 返回成功
     */
    @PostMapping("/auth/publish")
    public R publishVideo(@RequestBody @Valid PublishVideoReq req) {
        videoService.publishVideo(req, loginController.getUserSession().getUserId());
        return R.ok();
    }

    /**
     * 获取视频详情
     * @param videoId 视频Id
     * @return 视频详情
     */
    @GetMapping("/get")
    public R playVideoById(@RequestParam("videoId") String videoId) {
        PlayVideoByIdRes ans = videoService.getPlayVideoById(videoId, StpUtil.isLogin(), loginController.getUserSession());
        return R.okData(ans);
    }

    /**
     * 获取当前登录用户发布的视频列表
     * @param page 页码
     * @param size 页大小
     * @return 用户发布的视频列表
     */
    @GetMapping("/auth/list--own-videos")
    public R listOwnVideos(@RequestParam("page") int page, @RequestParam("size") int size) {
        List<OwnVideoRes> ans = videoService.listOwnVideos(page, size, loginController.getUserSession().getUserId());
        return R.okData(ans);
    }

    /**
     * 处理点赞视频请求
     */
    @RequestMapping("/auth/handle-like")
    public R handleLike(@RequestParam("videoId") String videoId) {
        int ans = videoService.handleLike(videoId, loginController.getUserSession().getUserId());
        return R.okData(ans);
    }

    /**
     * 处理视频播放量
     */
    @PostMapping("/handle-playCount")
    public R handlePlayCount(@RequestBody @Valid PlayCountReq req) {
        String playToken = videoService.handlePlayCount(req);
        return R.okData(playToken);
    }
}
